{include file="public/page_header" /} <!--头部公共区域-->

<div class="layui-layout layui-layout-admin">
    <div class="layui-row">
        <div class="layui-col-md3">
            <a href="#" onclick="addAdmin()" class="layui-btn layui-btn-danger">新增</a>
        </div>
    </div>
    <div class="table-responsive">
        <table id="table" lay-data="{id: 'idTest'}" lay-filter="test"></table>
    </div>
<!--    <div id="test1"></div>-->
</div>

{include file="public/page_footer" /} <!--尾部公共区域-->

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-sm" lay-event="admin_edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="admin_del">删除</a>
</script>
<script type="text/javascript">
    var reload;
    layui.use('table',function(){
        var table = layui.table;
        var laypage = layui.laypage;
        var tableIns = table.render({
            elem: '#table'
            //,height: 600
            ,url: "{:url('database/index')}" //数据接口
            ,page: false //开启分页
            ,id: 'idTest'
            // ,elem2: 'test1' //注意，这里的 test1 是 ID，不用加 # 号
            // ,limit: 15
            // ,count: 'count' //数据总数，从服务端得到
            ,cols: [[ //表头
                {type:'numbers', title: '序号', width:60}
                ,{field: 'Name', title: '表名',align:'center'}
                ,{field: 'Engine', title: '引擎',align:'center'}
                ,{field: 'Auto_increment', title: '自动递增值',align:'center'}
                ,{field: 'Data_length', title: '数据长度（KB）',align:'center'}
                ,{field: 'Rows', title: '行',align:'center'}
                ,{field: 'Collation', title: '字符集',align:'center'}
                ,{field: 'Create_time', title: '创建日期',align:'center'}
                ,{field: 'Update_time', title: '修改日期',align:'center'}
                ,{field: 'Comment', title: '注释',align:'center'}
                ,{field: 'right', title: '操作',align:'center',width:300,toolbar:"#barDemo"}
            ]]
        });
        // laypage.render({
        //     elem: 'test1' //注意，这里的 test1 是 ID，不用加 # 号
        //     ,count: 'count' //数据总数，从服务端得到
        // });

        //搜索
        $('#searchBtn').click(function(){
            reload();
        });

        //重载
        reload = function (){
            tableIns.reload({
                where: {
                    select:$('#type').val()
                }
                // ,page: {
                //     curr: 1 //重新从第 1 页开始
                // }
            });
        }

        //监听工具条
        table.on('tool(test)', function(obj){
            var data = obj.data;
            if(obj.event === 'admin_edit'){
                addAdmin(data.Name);
            }
            if(obj.event === 'admin_del'){
                if(data.Name === 'xt_admin' || data.Name === 'xt_admin_log' ||data.Name === 'xt_auth_group' ||data.Name === 'xt_auth_group_access' || data.Name === 'xt_auth_rule' || data.Name === 'xt_powerteam' || data.Name === 'xt_error_log' || data.Name === 'xt_config' || data.Name === 'xt_bank_province_city'){
                    layer.msg('系统表禁止删除!')
                    return false;
                }
                layer.confirm('确定要删除吗？',{
                    btn:['确定','取消']
                    ,btn2:function (index) {
                        layer.close(index)
                    }
                },function (index) {
                    $.ajax({
                        type : "POST",  //提交方式
                        url : "{:url('database/del')}",//路径
                        data : {
                            "name" : data.Name,
                        },
                        success: function(data){
                            if(data.code === 0){
                                layer.msg('操作成功！')
                                reload();
                            }else{
                                layer.msg('操作失败！')
                            }
                        }
                    })
                })
            }
        });
    });

    $('select').change(function () {
        reload();
    })

    function addAdmin(name=''){
        var url,sub_url,title
        if(name == ''){
            url = "{:url('database/add')}";
            sub_url = "{:url('database/add')}";
            title = '新增';
        }else{
            url = "{:url('database/edit')}?name=" + name;
            sub_url = "{:url('database/edit')}";
            title = '修改';
        }
        var index = layer.open({
            type: 2,
            title: title,
            id:"layer_addadmin",
            area: ['500px', '450px'],
            content: url,
            btn:["保存","关闭"],
            btn1:function () {
                var _$ = $("#layer_addadmin").find("iframe")[0].contentWindow.layui.jquery;
                var _form = $("#layer_addadmin").find("iframe")[0].contentWindow.layui.form;
                _form.on('submit(formDemo)', function(data){
                    var d = data.field;
                    $.ajax({
                        url:sub_url,
                        type:"post",
                        data: d,
                        success:function (ret) {
                            layer.msg(ret['msg']);
                            if(ret['code'] == 0){
                                setTimeout(function () {
                                    layer.close(index);
                                }, 2000);
                                reload();
                            }
                        }
                    });
                    return false;
                });
                _$("#formDemo").click();
            },
            btn2:function () {
                layer.close(index)
            }
        })
        // layer.full(index);
    }

</script>

